
import { _decorator, Component } from 'cc';
import { core } from '../core';
import { pushUnique, remove } from '../utils';
const { ccclass } = _decorator;

@ccclass('Mediator')
export default abstract class Mediator extends Component implements IMediator {

    protected onLoad(): void {
        pushUnique(core.mediators, this);
    }

    protected on(type: NotifyType, handler: NotifyHandler, priority: number = 0): void {
        core.on(type, handler, this, priority);
    }

    protected off(type: NotifyType, handler: NotifyHandler): void {
        core.off(type, handler, this);
    }
    protected notify(type: NotifyType, ...data: any[]): void {
        core.notify(type, ...data);
    }

    protected onDestroy(): void {
        core.unload(this);
        remove(core.mediators, this);
    }
}